<?php

namespace App\Services\Api;

use App\Models\GroupClass;
use App\Models\ClassBaseInfo;
use App\Models\GroupClassBaseInfo;
use App\Models\GroupClassInfo;
use App\Models\UserSubscribe;
use App\Services\Admin\Reservation;
use App\Traits\RetrunService;

class GroupClassServices
{
    /**
     * 獲取camp列表
     * @param $groupClass \App\Models\GroupClass
     */
    public function group_class_list($groupClass)
    {
        $data = $groupClass->with([
            'group_class_info' => function ($q) {
                $q->select(['id', 'class_base_info_id', 'group_class_id', 'plan_date'])->with([
                    'class_base_info' => function ($q) {
                        $q->select(['id', 'store_id'])->with([
                            'store' => function ($q) {
                                $q->select(['id', 'store_name']);
                            },
                        ]);
                    },
                ]);
            },

        ])
            ->orderBy('end_time', 'desc')
            ->paginate(100);

        $data_array = $data->toArray();

        //dd($data_array);
        if (count($data_array['data']) > 0) {
            $new_time = time();
            foreach ($data_array['data'] as $k => $v) {

                $registration_start_time = $v['registration_start_time'];
                $registration_time = $v['registration_time'];

                if (!empty($registration_start_time) && !empty($registration_time)) {
                    //檢查過期
                    if ($new_time < strtotime("{$registration_start_time}") || $new_time > strtotime("{$registration_time}")) {
                        unset($data_array['data'][$k]);
                        continue;
                    }
                }

                if (!empty($v['group_class_info'])) {

                    foreach ($v['group_class_info'] as $kk => $vv) {

                        //var_dump($vv);die;
                        if ($vv['plan_date'] < date("Y-m-d")) {
                            unset($data_array['data'][$k]);
                            //var_dump($data_array);die;
                            continue 2;
                        }
                    }

                } else {
                    unset($data_array['data'][$k]);
                    continue;
                }

                $data_array['data'][$k]['date_scope'] = date('Y-m-d', strtotime($v['start_time'])) . '至' . date('Y-m-d', strtotime($v['end_time']));
                $class_time_info = json_decode($v['class_time_info']);
                $class_weeks = array_column($class_time_info, 'class_weeks');
                $class_times = array_column($class_time_info, 'class_time');
                $data_array['data'][$k]['all_time'] = implode(',', $class_times);
                $data_array['data'][$k]['all_week'] = implode(',', $class_weeks);

                $data_array['data'][$k]['store_id'] = $v['group_class_info'][0]['class_base_info']['store']['id'];
                $data_array['data'][$k]['store_name'] = $v['group_class_info'][0]['class_base_info']['store']['store_name'];
                unset($data_array['data'][$k]['group_class_info']);
            }
        }

        $result = [];

        foreach ($data_array['data'] as $v){
            $result[$v['store_id']]['store_name'] = $v['store_name'];
            $result[$v['store_id']]['lists'][] = $v;
        }

        $data_array['data'] = array_values($result);
        return $data_array;
    }

    public function group_class_info($id)
    {
        $groupClass = new GroupClass();
        $data = $groupClass->where('id', $id)
            ->with([
                'group_class_info' => function ($q) {
                    $q->with([
                        'class_base_info' => function ($q) {
                            $q->with([
                                'class_base' => function ($q) {
                                    //$q->select(['id','class_name']);
                                },
                                'teacher' => function ($q) {
                                    $q->select(['id', 'teacher_name', 'teacher_head', 'synopsis', 'tab']);
                                },
                                'classsroom' => function ($q) {
                                    $q->select(['id', 'numbering']);
                                },
                                'store' => function ($q) {
                                    $q->select(['id', 'store_name']);
                                },
                            ]);
                        },
                    ]);
                },
            ])
            ->first()->toArray();

        $camp_info = \App\Services\Admin\GroupClass::camp_info($id)->get()->toArray();

        //用来计算课时
        $h_time = 0;

        foreach ($camp_info as $v) {
            foreach ($v['group_class_info'] as $vv) {
                $h_time++;
            }
        }

        if (!empty($data)) {
            $data['group_class_price'] = round($data['group_class_price']);
            $group_class_info = [];
            foreach ($data['group_class_info'] as $k => $v) {
                //dd($data,$v);
                $group_class_info['plan_date'] = $v['plan_date'];
                //$group_class_info['remedial_teaching_number'] = $v['remedial_teaching_number'];
                $group_class_info['class_base_info_id'] = $v['id'];
                $group_class_info['class_time'] = $v['class_base_info']['class_time'];

                $class_time = explode("~", $group_class_info['class_time']);

                $start_time_string = $class_time[0] ?? '';
                $end_time_string = $class_time[1] ?? '';

                $class_time_start_array = strtotime(date("Y-m-d {$start_time_string}:00"));
                $class_time_end_array = strtotime(date("Y-m-d {$end_time_string}:00"));

                $group_class_info['class_time_h'] = intval(($class_time_end_array - $class_time_start_array) / 3600 * $h_time);

                $group_class_info['class_week'] = $v['class_base_info']['class_week'];
                $v['class_base_info']['class_base']['class_price'] = round($v['class_base_info']['class_base']['class_price']);
                $group_class_info['class_base'] = $v['class_base_info']['class_base'];

                //用于查询上课老师
                $class_bass_info_teacher_arr = Reservation::class_bass_info_teacher_result($v['class_base_info']['id'])->get()->toArray();

                $teacher = Reservation::class_bass_info_teacher_arr($class_bass_info_teacher_arr);

                $group_class_info['teacher'] = $teacher;

                $group_class_info['classsroom'] = $v['class_base_info']['classsroom'];
                $group_class_info['store'] = $v['class_base_info']['store'];
                //$group_class_info['class_base']['class_base']['class_price'] = round($v['class_base_info']['class_base']['class_price']);
                $data['group_class_info'][$k] = $group_class_info;
                // $data['group_class_info'][$k]['class_base_info']['class_base']['class_price'] =  round($data['class_base']['class_price']);
            }


        }

        return $data;
    }

}
